<?php

namespace app\site\controller\routine;

use app\common\controller\Backend;
use pdd\PddJxc;

/**
 * 店铺管理
 */
class Shop extends Backend
{
    /**
     * Shop模型对象
     * @var object
     * @phpstan-var \app\site\model\routine\Shop
     */
    protected object $model;

    protected array|string $preExcludeFields = ['id'];

    protected string|array $quickSearchField = ['name'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\site\model\routine\Shop();

        $adminInfo = $this->auth->getInfo();
        if(!empty($adminInfo['shop_auth'])){
            $this->model->whereIn('id',$adminInfo['shop_auth']);
        }

    }


    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */


    /**
     * 同步店铺信息
     * @return void
     */
    public function sync()
    {
        $shopList = (new PddJxc($this->site_id))->getShopList();
        if(!$shopList) $this->error('暂时没有店铺信息');
        $this->model->where('site_id',$this->site_id)->delete();
        foreach ($shopList as $shop){
            $ret = $this->model->create([
                'site_id'=>$this->site_id,
                'id'=>$shop['shopId'],
                'name'=>$shop['shopName'],
                'platform_shop_id' => $shop['platformShopId']
            ]);
        }
        if(!$ret) $this->error('同步失败');
        $this->success('同步成功');
    }



}